﻿CREATE OR REPLACE FUNCTION change_project_currency() RETURNS trigger AS '
DECLARE
  changed boolean;
BEGIN
  IF tg_op = ''INSERT'' THEN
     changed := TRUE;
  ELSE
     changed := new.price IS NULL != old.price IS NULL OR new.price != old.price;
  END IF;
  IF changed THEN
     INSERT INTO price_change(apn, effective, price)
     VALUES (new.barcode, CURRENT_TIMESTAMP, new.price);
  END IF;
  RETURN new;
END
' LANGUAGE plpgsql;

--CREATE TRIGGER insert_price_change AFTER INSERT OR DELETE OR UPDATE ON stock
  --FOR EACH ROW EXECUTE PROCEDURE insert_price_change();